package cn.didi.feidi.location.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import cn.didi.feidi.location.R;


/**
 * 自定义的对话框（单按钮与双按钮）
 *
 */

public class CommomDialog extends Dialog implements View.OnClickListener {

//    设置 是否单按钮 或者 隐藏Title
    private Boolean isSingleButton = false;
    private Boolean isCloseTitle = false;

    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    private View splitLine;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;

    private String submitTxtColor = "#212121";
    private String cancelTxtColor = "#212121";

    public CommomDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public CommomDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener, Boolean isSingleButton, Boolean isCloseTitle) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
        this.isSingleButton = isSingleButton;
        this.isCloseTitle = isCloseTitle;
    }

    protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CommomDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommomDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public CommomDialog setPositiveButton(String name, String color) {
        this.positiveName = name;
        this.submitTxtColor = color;
        return this;
    }

    public CommomDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    public CommomDialog setNegativeButton(String name, String color) {
        this.negativeName = name;
        this.cancelTxtColor = color;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_commom);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        contentTxt = (TextView) findViewById(R.id.commom_dialog_content);
        titleTxt = (TextView) findViewById(R.id.commom_dialog_title);
        submitTxt = (TextView) findViewById(R.id.commom_dialog_submit);
        cancelTxt = (TextView) findViewById(R.id.commom_dialog_cancel);
        splitLine = findViewById(R.id.split_line_for_button);

        contentTxt.setText(content);
        if (!TextUtils.isEmpty(positiveName)) {
            submitTxt.setTextColor(Color.parseColor(submitTxtColor));
            submitTxt.setText(positiveName);
        }

        if (!TextUtils.isEmpty(negativeName)) {
            cancelTxt.setTextColor(Color.parseColor(cancelTxtColor));
            cancelTxt.setText(negativeName);
        }

        if (isCloseTitle) {
            titleTxt.setVisibility(View.GONE);
        } else {
            if (!TextUtils.isEmpty(title)) {
                titleTxt.setText(title);
            }
        }

        submitTxt.setOnClickListener(this);

        if (isSingleButton) {
            splitLine.setVisibility(View.GONE);
            cancelTxt.setVisibility(View.GONE);
            submitTxt.setBackgroundResource(R.drawable.common_dialog_only_show_right_select);
            return;
        }

        cancelTxt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.commom_dialog_cancel){
            if (listener != null) {
                listener.onClick(this, false);
            }
            this.dismiss();
        }else if(v.getId()==R.id.commom_dialog_submit){
            if (listener != null) {
                listener.onClick(this, true);
            }
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm);
    }
}